﻿using System;
using System.Collections.Generic;
using System.Web.Services;
using StampController.BL;
using StampController.BL.Models;
using StampController.Pages;
using log4net;
using System.Net;

namespace StampController
{
    public partial class _Default : BasePage
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(_Default).Name);

        private const int PageSize = 20;

        protected void Page_Load(object sender, EventArgs e)
        {
            var currentuser = SessionUtils.Get<UsersModel>("USER");
            logger.Debug(currentuser.UserID + " has just logged in on " + _getClientHostName() + " (IP: " + _getClientAddress() + ")");
        }

        [WebMethod]
        public static object GetRequestFormList(int pageNum, string keyword)
        {
            var user = SessionUtils.Get<UsersModel>("USER");
            int count;
            var im = new RequestFormImpl();
            IList<RequestFormModel> list = im.GetRequestFormList(user.UserID, keyword, null, null, pageNum, PageSize,
                out count);

            var obj = new GridTable<RequestFormModel>
            {
                PageNum = pageNum,
                RowCount = count,
                PageSize = PageSize,
                List = list,
                Approval = false
            };

            return obj;
        }

        [WebMethod]
        public static bool DeleteItem(int id)
        {
            var im = new RequestFormImpl();
            return im.DeleteRequestForm(id);
        }

        private string _getClientHostName()
        {
            return Dns.GetHostEntry(Request.ServerVariables["REMOTE_ADDR"]).HostName;
        }

        private string _getClientAddress()
        {
            IPAddress[] iplist= Dns.GetHostAddresses(Request.ServerVariables["REMOTE_HOST"].ToString());
            return iplist[0].ToString();
        }
    }
}